package org.fone.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import org.fone.service.impl.UserServiceImpl;
import org.fone.utils.R;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class UserController {

    private final UserServiceImpl userService;

    public UserController(UserServiceImpl userService) {
        this.userService = userService;
    }

    @ResponseBody
    @PostMapping("/setDragInfo")
    R setDragInfo(@RequestBody String data){
        System.out.println("data = " + data);
        try {
            JSONObject dataObj = new JSONObject(data);
            String save_id = dataObj.getStr("save_id");
            if (save_id==null||"".endsWith(save_id)) return R.error("没有输入 save_id");

            if (userService.saveDragInfo(save_id,dataObj.getStr("data"))) return R.ok();
            else return R.error("数据库连接错误");
        }catch (Exception e){
            return R.error("只能保存json数据格式");
        }
    }
    /*
        {-1=Selector{source_name='基本信息', type=SELECTOR, task_id=-1}, -2=Selector{source_name='学历信息', type=SELECTOR, task_id=-2}, -3=Filter{source_id=-1, field_type='STR', field='工号', operator='contains', value='E', type=FILTER, task_id=-3}, -4=Merger{source_ids=[-2, -3], match_relationship={身份证号=身份证号}, out_fields=[[Ljava.lang.String;@30d3e585, [Ljava.lang.String;@6302b5ac, [Ljava.lang.String;@75bb98dd, [Ljava.lang.String;@4e10e38a, [Ljava.lang.String;@61c0c800, [Ljava.lang.String;@1799ae41, [Ljava.lang.String;@3ecda11f, [Ljava.lang.String;@778819ff, [Ljava.lang.String;@10ef5fc6, [Ljava.lang.String;@4ade003c, [Ljava.lang.String;@19489521], type=MERGER, task_id=-4}, -5=End{source_id=0, type=END, task_id=-5}}
{-1=[-3], -2=[-4], -3=[-4], -4=[-5]}
        {-1=Selector{source_name='基本信息', type=SELECTOR, task_id=-1}, -2=Selector{source_name='学历信息', type=SELECTOR, task_id=-2}, -3=Merger{source_ids=[-4, -2], match_relationship={身份证号=身份证号}, out_fields=[[Ljava.lang.String;@71feb715, [Ljava.lang.String;@4147a724, [Ljava.lang.String;@1108d24f, [Ljava.lang.String;@1e583bd8, [Ljava.lang.String;@1a1a7119, [Ljava.lang.String;@66989bf9, [Ljava.lang.String;@31d5a7cf, [Ljava.lang.String;@6e0cccd6, [Ljava.lang.String;@27d7fab7, [Ljava.lang.String;@532fae7, [Ljava.lang.String;@19c5ce51], type=MERGER, task_id=-3}, -4=Filter{source_id=-1, field_type='STR', field='工号', operator='contains', value='E', type=FILTER, task_id=-4}, -5=End{source_id=0, type=END, task_id=-5}}
{-1=[-4], -2=[-3], -3=[-5], -4=[-3]}
    */
    @ResponseBody
    @GetMapping("/getDragInfo")
    R getDragInfo(String save_id){
        if (save_id==null||"".endsWith(save_id)) return R.error("后台没有您的数据");
        System.out.println("save_id = " + save_id);
        JSONObject data = userService.getDragInfo(save_id);
        if (ObjectUtil.isNotEmpty(data)) return R.ok().put("data",data);
        else return R.error("后台没有您的数据");
    }
}
